// pages/parking/parking.js
let app = getApp()
let util = require("../../utils/util")
let request = require("../../utils/request")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageAllHeight: app.globalData.pageAllHeight,
    datePickerDialog: false,
    nowDate: {},
    currentPage: 1,
    pageSize: 15,
    pageFlag: false,
    parkInfo: {},
    stopCarList: [],
    parkId: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let date = util.formatTime(new Date())
    this.setData({
      nowDate: date,
      parkId: wx.getStorageSync('lmtc_admin_parkInfo').parkId
    })
    this.getParkMonthRevenue()
    this.getMonthParking()
    this.getNowParking()
    this.getParkOrderInfo()
  },
  /**
   * 本月收入数
   */
  getParkMonthRevenue() {
    let that = this
    let parmas = {
      parkId: this.data.parkId
    }
    request.http('/order/orderInfo/getParkMonthRevenue', 'GET', parmas)
      .then(res => {
        let data = res.data
        if (data.code == 200) {
          that.setData({
            'parkInfo.monthRevenue': data.data
          })
        }
      })
  },
  /**
   * 本月泊车数
   */
  getMonthParking() {
    let that = this
    let parmas = {
      parkId: this.data.parkId
    }
    request.http('/order/orderInfo/getMonthParking', 'GET', parmas)
      .then(res => {
        let data = res.data
        if (data.code == 200) {
          that.setData({
            'parkInfo.monthParking': data.data
          })
        }
      })
  },
  /**
   * 场内车辆数
   */
  getNowParking() {
    let that = this
    let parmas = {
      parkId: this.data.parkId
    }
    request.http('/order/orderInfo/getNowParking', 'GET', parmas)
      .then(res => {
        let data = res.data
        if (data.code == 200) {
          that.setData({
            'parkInfo.carCount': data.data
          })
        }
      })
  },
  /**
   * 获取停车场订单信息
   */
  getParkOrderInfo(next) {
    let that = this
    let nowDate = that.data.nowDate
    let parmas = {
      page: that.data.currentPage,
      size: that.data.pageSize,
      parkId: that.data.parkId,
      selectDate: nowDate.year + '-' + nowDate.month + '-' + nowDate.day
    }
    request.http('/order/orderInfo/getOrderInfoByDate', 'POST', parmas,0)
      .then(res => {
        let data = res.data
        let stopCarList = that.data.stopCarList
        let pageFlag = that.data.pageFlag
        if (data.code == 200) {
          if (next) {
            if (data.data.records.length > 0) {
              stopCarList = stopCarList.concat(data.data.records)
              pageFlag = false
            } else {
              pageFlag = true
            }
          } else {
            stopCarList = data.data.records
          }
          that.setData({
            stopCarList,
            pageFlag
          })
        }
      })
  },
  /**
   * 显示时间选择器弹窗
   */
  showDatePickerDialog() {
    this.setData({
      datePickerDialog: true
    })
  },
  /**
   * 点击时间选择器确定按钮
   */
  onClickCancelDatePicker(e) {
    let data = e.detail.value
    this.setData({
      nowDate: {
        year: data[0],
        month: data[1],
        day: data[2]
      },
      currentPage: 1
    })
    this.getParkOrderInfo()
  },
  /**
   * 触底事件
   */
  scrollTolower() {
    if (this.data.pageFlag) {
      return
    }
    this.setData({
      pageFlag: true,
      currentPage: this.data.currentPage + 1
    })
    this.getParkOrderInfo(true)
  }
})